package ex.com.jm.food.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;


import ex.com.jm.food.dao.CompanyMenuDao;
import ex.com.jm.food.entity.Menu;
import ex.com.jm.food.entity.PicStyle;
/**
 * 菜单管理接口实现
 * @author wangshenfeng
 *
 */
public class CompanyMenuDaoImpl extends HibernateDaoSupport implements
		CompanyMenuDao {

	@Override
	public Object saveMenu(Menu m) {
		// TODO Auto-generated method stub
		Object c = this.getHibernateTemplate().save(m);
		return c;
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<Menu> selectMenu(final int initPage, final int pageRow, final String name,
			final Integer type, final Integer status,final String comID, final String s_date,
			final String e_date,final String order,final String sort) {
		// TODO Auto-generated method stub
		
		
		@SuppressWarnings("rawtypes")
		Object object=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="SELECT new ex.com.jm.food.entity.dto.ComMenuDTO(menu.menuId,"
						+ "menu.menuName,"
						+ "menu.status,"
						+ "CASE WHEN menu.menuType=0 THEN '普通菜' WHEN menu.menuType=1 THEN '特价菜' WHEN menu.menuType=2 THEN '热销菜' WHEN menu.menuType=3 THEN '新菜品' END,"
						+ "menu.menuDate,"
						+ " menu.menuInDate,"
						+ "menu.menuNowPrice)"
						+ " FROM Menu AS menu"
						+ " WHERE menu.comId='"+comID+"' ";
				if(name!=null&&!name.equals("")){
					hql+=" AND menu.menuName LIKE '%"+name+"%'";
				}
				if(type!=null&&!type.equals("")){
					hql+=" AND menu.menuType="+type;
				}
				if(status!=null&&!status.equals("")){
					hql+=" AND menu.status="+status;
				}
				if(s_date!=null&&!s_date.equals("")){
					hql+=" AND menu.menuDate>='"+s_date+"'";
				}
				if(e_date!=null&&!e_date.equals("")){
					hql += " AND menu.menuDate<='"+e_date+"'";
					
				}if(sort!=null&&!sort.equals("")&&order!=null&&!order.equals("")){
					hql +=" ORDER BY "+order+" "+sort;
				}else {
					hql+=" ORDER BY menu.menuDate DESC";
				}
				Query query=session.createQuery(hql).setFirstResult(initPage).setMaxResults(pageRow);
				return query.list();
			
			}
		});
		return (List<Menu>) object;

	}

	@Override
	public void deleteMenu(String mID) {
		// TODO Auto-generated method stub
		try {
			this.getHibernateTemplate().delete(this.getHibernateTemplate().get(Menu.class, mID));
			this.getHibernateTemplate().delete(mID);
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public int menuConut(final String comID, final String menuName, final Integer type,final Integer status,
			final String s_date, final String e_date) {
		// TODO Auto-generated method stub
	int count=	this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from Menu where comId='"+comID+"' ";
				if (menuName!=null&&menuName.equals("")) {
				hql+="and menuName like '%"+menuName+"%'"	;
				}if(status!=null&&!status.equals("")){
					hql+=" AND status="+status;
				}if (type!=null && !type.equals("")) {
					hql+=" AND menuType="+type;
				}
				if(s_date!=null&&!s_date.equals("")){ 
					hql+=" AND menuDate>='"+s_date+"'";
				}
				if(e_date!=null&&!e_date.equals("")){
					hql += " AND menuDate<='"+e_date+"'";
				}
				Query query=session.createQuery(hql);
				
				return query.list().size();
			}
		});
		return count;
	}




	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<PicStyle> getPicStyles() {
		// TODO Auto-generated method stub
		Object object=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from PicStyle";
				Query query=session.createQuery(hql);
				return query.list();
			}
		});
		return (List<PicStyle>)object;
	}

	@Override
	public Menu editMenu(String menuId) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(Menu.class, menuId);
	}


	@Override
	public void updateMenu(Menu menu) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().update(menu);
	}

	

}
